package org.jeecg.modules.hc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.hc.entity.HcCompanyMerchantAfterSales;
import org.jeecg.modules.hc.entity.HcCompanyMerchantSettlement;
import org.jeecg.modules.hc.enums.HcAfterSalesStateEnum;
import org.jeecg.modules.hc.enums.HcMerchantSettlementTypeEnum;
import org.jeecg.modules.hc.mapper.HcCompanyMerchantAfterSalesMapper;
import org.jeecg.modules.hc.mapper.HcCompanyMerchantSettlementMapper;
import org.jeecg.modules.hc.service.IHcCompanyMerchantAfterSalesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Description: 企业订餐售后
 * @Author: jeecg-boot
 * @Date:   2023-07-15
 * @Version: V1.0
 */
@Service
@AllArgsConstructor
public class HcCompanyMerchantAfterSalesServiceImpl extends ServiceImpl<HcCompanyMerchantAfterSalesMapper, HcCompanyMerchantAfterSales> implements IHcCompanyMerchantAfterSalesService {

    private HcCompanyMerchantSettlementMapper hcCompanyMerchantSettlementMapper;
    private HcCompanyMerchantAfterSalesMapper hcCompanyMerchantAfterSalesMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(HcCompanyMerchantAfterSales hcCompanyMerchantAfterSales) {

        HcCompanyMerchantAfterSales companyMerchantAfterSales = this.getById(hcCompanyMerchantAfterSales.getId());

        if (!companyMerchantAfterSales.getState().equals(HcAfterSalesStateEnum.NO)) {
            throw new JeecgBootException("订单已处理");
        }
        HcCompanyMerchantAfterSales companyMerchantAfterSalesNew = new HcCompanyMerchantAfterSales();

        companyMerchantAfterSalesNew.setId(hcCompanyMerchantAfterSales.getId());
        companyMerchantAfterSalesNew.setState(hcCompanyMerchantAfterSales.getState());
        companyMerchantAfterSalesNew.setProcessingTime(new Date());
        if (hcCompanyMerchantAfterSales.getState().equals(HcAfterSalesStateEnum.YES)) {
            //todo  其他逻辑同意逻辑
            companyMerchantAfterSalesNew.setDeductMerchantAmount(hcCompanyMerchantAfterSales.getDeductMerchantAmount());
            companyMerchantAfterSalesNew.setDeductionRemarks(hcCompanyMerchantAfterSales.getDeductionRemarks());
            //各商家增加一条变动结算信息
            HcCompanyMerchantSettlement hcCompanyMerchantSettlement = new HcCompanyMerchantSettlement();
            hcCompanyMerchantSettlement.setMerchantId(hcCompanyMerchantAfterSales.getMerchantId());
            hcCompanyMerchantSettlement.setCompanyId(hcCompanyMerchantAfterSales.getCompanyId());
            hcCompanyMerchantSettlement.setSettlementAmount(new BigDecimal("-"+hcCompanyMerchantAfterSales.getDeductMerchantAmount()));
            hcCompanyMerchantSettlement.setSettlementDate(new Date());
            hcCompanyMerchantSettlement.setChangeRemarks(hcCompanyMerchantAfterSales.getDeductionRemarks());
            hcCompanyMerchantSettlement.setCreateTime(new Date());
            hcCompanyMerchantSettlement.setUpdateTime(new Date());
            hcCompanyMerchantSettlement.setType(HcMerchantSettlementTypeEnum.CHANGE);
            hcCompanyMerchantSettlementMapper.insert(hcCompanyMerchantSettlement);

        }else if (hcCompanyMerchantAfterSales.getState().equals(HcAfterSalesStateEnum.REJECT)){
            if (StringUtils.isBlank(hcCompanyMerchantAfterSales.getReasonRejection())) {
                throw new JeecgBootException("请填写驳回原因");
            }
            companyMerchantAfterSalesNew.setReasonRejection(hcCompanyMerchantAfterSales.getReasonRejection());
        }
        this.updateById(companyMerchantAfterSalesNew);
    }

    @Override
    public IPage<HcCompanyMerchantAfterSales> getMerchantList(Page<HcCompanyMerchantAfterSales> page, HcCompanyMerchantAfterSales param) {
        return hcCompanyMerchantAfterSalesMapper.getMerchantList(page, param);
    }
}
